
package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.IEntity;
import com.xbongbong.saas.domain.dao.DayHealthDao;
import com.xbongbong.saas.domain.entity.DayHealthEntity;
import com.xbongbong.saas.model.DayHealthModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* @author chy
* @time 2018-07-04 14:01:41
*/
@Service("dayHealthModel")
public class DayHealthModelImpl implements DayHealthModel {

	@Resource
	private DayHealthDao dayHealthDao;

	private static final Logger LOG = LoggerFactory.getLogger(DayHealthModelImpl.class);

	@Override
	public Integer save(DayHealthEntity dayHealthEntity) {
		if (dayHealthEntity.getId() == null || dayHealthEntity.getId().equals(0)) {
			return insert(dayHealthEntity);
		}
		return update(dayHealthEntity);
	}

	@Override
	public List<DayHealthEntity> findByEntryTimeAndCorpid(Map<String, Object> param) {
		return dayHealthDao.findByEntryTimeAndCorpid(param);
	}

	@Override
	public Integer insert(DayHealthEntity dayHealthEntity){
		Long now = DateTimeUtil.getInt();
		dayHealthEntity.setAddTime(now);
		dayHealthEntity.setUpdateTime(now);
		return dayHealthDao.insert(dayHealthEntity);
	}

	@Override
	public Integer update(DayHealthEntity dayHealthEntity){
		dayHealthEntity.setUpdateTime(DateTimeUtil.getInt());
		return dayHealthDao.update(dayHealthEntity);
	}

	@Override
	public void insertBatch(List<DayHealthEntity> entityList) {
		dayHealthDao.insertBatch(entityList);
    }

	@Override
	public Integer deleteByKey( Integer key,  String corpid){
		return dayHealthDao.deleteByKey(key , corpid);
	}

	@Override
	public DayHealthEntity getByKey( Integer key,  String corpid){
		return dayHealthDao.getByKey(key , corpid);
	}

	@Override
	public List<DayHealthEntity> findEntitys(Map<String ,Object>  param){
		return dayHealthDao.findEntitys(param);
	}
    @Override
	public Integer getEntitysCount(Map<String ,Object>  param){
		return dayHealthDao.getEntitysCount(param);
	}

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }


    @Override
	public DayHealthEntity getEntityByEntryDate(String corpid, Long entryDate) {
		entryDate = DateTimeUtil.getTodayInt(entryDate);
		Map<String, Object> param = new HashMap<>();
		param.put("corpid", corpid);
		param.put("entryDate", entryDate);
		param.put("del", 0);

		Integer count = getEntitysCount(param);
		if(count == 0) {
			return null;
		}
		List<DayHealthEntity> list = findEntitys(param);
		return list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public List<DayHealthEntity> getListByTime(String corpid, Integer startTime, Integer endTime) {
		Map<String, Object> param = new HashMap<>();
		param.put("corpid", corpid);
		param.put("entryDateStart", startTime);
		param.put("entryDateEnd", endTime + 1);
		param.put("del", 0);
		List<DayHealthEntity> list = findEntitys(param);
		return list;
	}
}

